/* 
 *  Copyright 2009-2010 Michal Trzcinka
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software distributed
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 */
YAHOO.namespace('TRZCINKA.VCARD');

(function() {

  var Dom = YAHOO.util.Dom,
  Lang = YAHOO.lang,
  Config = YAHOO.TRZCINKA.VCARD.Config,
  Event = YAHOO.util.Event,
  Constants = YAHOO.TRZCINKA.VCARD.Constants,
    DDTarget = YAHOO.util.DDTarget,
  LOGGER = new YAHOO.widget.LogWriter('cell.js');

  /**
   * Cell
   */

  YAHOO.TRZCINKA.VCARD.Cell = function(location, gridModel) {
    this._item = null;
    this._gridModel = gridModel;

    this.location = location;
    this.htmlElement = document.createElement('td');
    Dom.setStyle(this.htmlElement, 'width', Config.CELL_WIDTH + 'px');
    this.htmlElement.id = location.gridLocationToString();
    new DDTarget(this.htmlElement.id, Constants.DD_GROUP);

    Event.on(this.htmlElement, 'click', function() {
      if (gridModel.getMode() == Constants.MODE_DUPLICATING && !gridModel.itemExists(location)) {
        gridModel.duplicateItem(location);
      } else if (gridModel.getMode() == Constants.MODE_LINKING) {
        gridModel.cancelLinking();
      }
    });
  }


  YAHOO.TRZCINKA.VCARD.Cell.prototype.itemSaved = function(data) {
    var stacking = this._item != null;
    if (stacking) {
      this._item.destroy();
      this._item = null;
      this._markAsStack();
    }
    this._item = new YAHOO.TRZCINKA.VCARD.Item(this._gridModel, this, data);
    this.htmlElement.appendChild(this._item.htmlElement);
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.itemRemoved = function() {
    this._item.destroy();
    this._item = null;

    var top = this._gridModel.getTopItem(this.location);
    if (top) {
      this.itemSaved(top);
    }

    if (!this._gridModel.isStack(this.location)) {
      this._unmarkAsStack();
    }
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.cleared = function() {
    if (this._item) {
      this._item.destroy();
    }
    this._item = null;
  }

  YAHOO.TRZCINKA.VCARD.Cell.prototype.itemMoved = function(targetCell) {
    targetCell.itemSaved(this._gridModel.getTopItem(targetCell.location));
    this.itemRemoved();
    if (!this._gridModel.isStack(this.location)) {
      this._unmarkAsStack();
    }
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.stackMoved = function(targetCell) {
    targetCell.itemSaved(this._gridModel.getTopItem(targetCell.location));
    this._item.destroy();
    this._item = null;
    this._unmarkAsStack();
    targetCell._markAsStack();
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.updated = function(data) {
    this._item.updated(data);
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.highlight = function() {
    LOGGER.log('highlight cell', 'debug');
    this.htmlElement.style.borderWidth = 5;
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.disableHighlight = function() {

  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype.topItemSet = function(data) {
    this.itemSaved(this._gridModel.getTopItem(data.location));
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype._markAsStack = function() {
    this.htmlElement.style.backgroundColor = Config.STACK_COLOR;
  };

  YAHOO.TRZCINKA.VCARD.Cell.prototype._unmarkAsStack = function() {
    this.htmlElement.style.backgroundColor = 'transparent';
  };


  YAHOO.TRZCINKA.VCARD.Cell.prototype.toString = function() {
    return Lang.dump(this.location);
  };

})();
